#!/bin/bash

check_command() {
    type $1 &> /dev/null || { echo "$1 not found." >&1; return 1; }
}

check_command expect || exit 127
check_command python || exit 127

if [ $# -eq 0 ]; then
    echo -e "provide services in SSH forwarding tunnel"
	echo -e "Usage:\n\t$0 [-i][-c][-p] <setting file> [vault file]"
    echo -e "Options:"
    echo -e "\t-i        show config info and exit."
    echo -e "\t-c        clean all processes and exit."
    echo -e "\t-p        type password."
    echo -e "visit https://github.com/wanghuiict/sshforward2/"
	exit 0
fi

onlyshow=
clean=
typepass=
while getopts ":icp" opt; do
    case $opt in
      i ) onlyshow="-i"
          ;;
      c ) clean="-c"
          ;;
      p ) typepass="-p"
          ;;
      ? ) echo "error input"
          exit 1;;
    esac
done
shift $(($OPTIND - 1))

. functions
# test
#echo "[$1] [$2]"
vault=$2
[ -n "$vault" ] || vault=default
get_vault_file $vault &> /dev/null || { echo "failed to get vault file: $vault" >&1; exit 1; }

# vault file located in ~/.vault/
vault=$(basename $vault)

if [ "$typepass" == "-p" ]; then
exec /bin/bash connector $onlyshow $clean $typepass $1 $vault
else
exec /bin/bash connector $onlyshow $clean $typepass $1 $vault &
fi

